package com.wuyi.tea.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuyi.tea.domain.pojo.Address;
import com.wuyi.tea.domain.vo.R;
import com.wuyi.tea.service.AddressService;
import com.wuyi.tea.util.LocalStorageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(tags = "收货地址")
@RestController
@RequestMapping("/user/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @ApiOperation("添加收货地址")
    @PostMapping
    public R addAddress(@RequestBody Address address){
        log.info("id为{}的用户添加了收获地址:\n{}",LocalStorageUtil.get(),address);
        address.setUserId(Long.valueOf(LocalStorageUtil.get().toString()));
        addressService.save(address);
        return R.success();
    }


    @ApiOperation("修改收货地址")
    @PutMapping
    public R updateAddress(@RequestBody Address address){
        log.info("id为{}的用户修改了收获地址:\n{}",LocalStorageUtil.get(),address);
        address.setUserId(Long.valueOf(LocalStorageUtil.get().toString()));
        addressService.updateById(address);
        return R.success();
    }


    @ApiOperation("删除收货地址")
    @DeleteMapping
    public R deleteAddress(@RequestParam Long id){
        log.info("id为{}的用户删除了收获地址{}",LocalStorageUtil.get(),id);
        addressService.removeById(id);
        return R.success();
    }


    @ApiOperation("查看收货地址")
    @GetMapping
    public R getAddress(@RequestParam Long id){
        log.info("id为{}的用户查看了收货地址{}",LocalStorageUtil.get(),id);
        Address address = addressService.getById(id);
        return R.success(address);
    }

    @ApiOperation("查看地址列表")
    @GetMapping("/list")
    public R list(){
        log.info("id为{}的用户查看了地址列表",LocalStorageUtil.get());
        List<Address> addressList= addressService.list(new LambdaQueryWrapper<Address>()
                .eq(Address::getId, Long.valueOf(LocalStorageUtil.get().toString()))
        );
        return R.success(addressList);
    }


}
